<?php
namespace app\admin\controller;
use app\admin\controller\Common;
class Shop extends Common
{
	function index(){
		$city = db('city')->where("count=1")->order('sort')->select();//省级
		$this->assign('city',$city);
		return view();
	}

	function ajax_shop_list(){
		$get_data = input("get.");
		$data = input('post.');

		$p = empty($_GET['p']) ? 1 : $_GET['p'];
        $size = empty($_REQUEST['size']) ? 25 : $_REQUEST['size'];
        $where = "1=1";
        if ($data['kind_id'])
        	$where .= " and kid=".$data['kind_id'];
        // 关键词搜索
        $keyword = $data['keyword'] ? trim($data['keyword']) : '';
        if($keyword)
        	$where .= " and title like '%".$keyword."%'";
        // var_dump($where);

        $count = db('shop')->where($where)->count();// 查询满足要求的总记录数
        $list = db('shop')->where($where)->order('sort,id desc')->paginate($size,$count)->each(function($item,$key){
        	return $item;
        });
        $page = $list->render();

        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$page);// 赋值分页输出
        $this->assign('count',$count);// 数据总数

		return view();
	}

	function shop_add(){
		if(request()->isAjax()){
			$post_data = input('post.');
			// var_dump($post_data);exit;
			$data['city_id'] = $post_data['city_id'];
			if($data['city_id'] == ''){
				$json = array('status'=>0,'message'=>'请选择城市');
				echo json_encode($json);exit;
			}
			$city = db('City')->where("id=".$data['city_id'])->find();
			$data['city_name'] = $city['name'];
			$data['title'] = $post_data['title'];
			$data['e_title'] = $post_data['e_title'];
			$data['add_site'] = $post_data['add_site'];
			$data['address'] = $post_data['address'];
			$data['map_x'] = $post_data['map_x'];
			$data['map_y'] = $post_data['map_y'];
			$data['tel'] = $post_data['tel'];
			$data['shop_time'] = $post_data['shop_time'];
			// $data['add_site'] = $post_data['add_site'];
			$s_img = $post_data['slide_img'];
			$s_alt = $post_data['slide_alt'];
			for($i=0;$i<count($s_img);$i++){
				$content[] = array(
					'image'=>$s_img[$i],
					'alt'=>$s_alt[$i],
				);
			}
			$data['content'] = json_encode($content);
			$res = db('Shop')->insertGetId($data);
			if($res){
				$json = array('status'=>1,'message'=>'添加门店成功');
				/*********************记录日志***********************/
            	addLog(session('user_name').'添加门店成功-'.$data['title'].' [ ID:'.$res.' ]');
			}else{
				$json = array('status'=>0,'message'=>'添加门店失败');
			}
			echo json_encode($json);exit;
		}else{
			$city = db('City')->where("count=1")->order('sort')->select();//省级
			$this->assign('city',$city);
			return view();
		}
	}

	function shop_edit(){
		if(request()->isAjax()){
			$post_data = input('post.');
			// var_dump($post_data);exit;
			$id = $post_data['id'];
			$data['city_id'] = $post_data['city_id'];
			if($data['city_id'] == ''){
				$json = array('status'=>0,'message'=>'请选择城市');
				echo json_encode($json);exit;
			}
			$city = db('City')->where("id=".$data['city_id'])->find();
			$data['city_name'] = $city['name'];
			$data['title'] = $post_data['title'];
			$data['e_title'] = $post_data['e_title'];
			$data['address'] = $post_data['address'];
			$data['add_site'] = $post_data['add_site'];
			$data['map_x'] = $post_data['map_x'];
			$data['map_y'] = $post_data['map_y'];
			$data['tel'] = $post_data['tel'];
			$data['shop_time'] = $post_data['shop_time'];
			$s_img = $post_data['slide_img'];
			$s_alt = $post_data['slide_alt'];
			for($i=0;$i<count($s_img);$i++){
				$content[] = array(
					'image'=>$s_img[$i],
					'alt'=>$s_alt[$i],
				);
			}
			$data['content'] = json_encode($content);
			$res = db('Shop')->where("id=".$id)->update($data);
			if($res !== false){
				$json = array('status'=>1,'message'=>'编辑门店成功');
				/*********************记录日志***********************/
            	addLog(session('user_name').'编辑门店成功-'.$data['title'].' [ ID:'.$id.' ]');
			}else{
				$json = array('status'=>0,'message'=>'编辑门店失败');
			}
			echo json_encode($json);exit;
		}else{
			$id = input('route.id');
			$one = db('Shop')->where("id=".$id)->find();
			$city = db('City')->where("count=1")->order('sort')->select();//省级
			$city_now = db('City')->where("id=".$one['city_id'])->find();
			// var_dump($one);
			// var_dump($city_now);exit;
			//如果是市级
			if($city_now['count'] == 2){
				$sheng_now = db('City')->where("id=".$city_now['pid'])->find();//找省级
				$shi_now = $city_now;
				$shi = db('City')->where("pid=".$sheng_now['id'])->select();//同级市
				$this->assign('sheng_now',$sheng_now);
				$this->assign('shi_now',$shi_now);
				$this->assign('shi',$shi);
			}else{
				$sheng_now = $city_now;
				$this->assign('sheng_now',$sheng_now);
			}
			// var_dump($sheng_now);exit;
			$this->assign('city',$city);
			$this->assign('one',$one);
			return view();
		}
	}

	function shop_del(){
		$id = input('post.id');
		$data = db('Shop')->where("id=".$id)->find();
		$res = db('Shop')->where("id=".$id)->delete();
		if($res){
			$json = array('status'=>1,'message'=>'删除门店成功');
			/*********************记录日志***********************/
            addLog(session('user_name').'删除门店成功-'.$data['title'].' [ ID:'.$id.' ]');
		}else{
			$json = array('status'=>0,'message'=>'删除门店失败');
		}
		echo json_encode($json);exit;
	}

	public function get_son($val=''){
    	if(!request()->isAjax() || $val<1){
    		$this->redirect('Shop/index');
    	}
    	$where['pid'] = $val;
    	$res = db('City')->where($where)->select();
    	if($res){
    		$data['category'] = $res;
    		echo json_encode($data);exit;
    	}
    }








}
?>